package com.example.CarManagement.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.sys.entity.*;
import com.example.CarManagement.sys.mapper.ApplicationsMapper;
import com.example.CarManagement.sys.mapper.DriversMapper;
import com.example.CarManagement.sys.mapper.NotificationMapper;
import com.example.CarManagement.sys.service.INotificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements INotificationService {

    @Resource
    private UserServiceImpl userService;

    @Resource
    private SendMessageService sendMessageService;


    @Override
    public Result<?> selfNotice(String token) {
        LoginUser userInfo = userService.getUserInfo(token);

        User user = userInfo.getUser();

        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notification::getUserId, user.getId());
        queryWrapper.orderByDesc(Notification::getCreateTime);
        List<Notification> notifications = this.baseMapper.selectList(queryWrapper);

        return Result.success(notifications);
    }


    @Override
    public Result<?> add(Notification notification, String token) {

        notification.setId(UUID.randomUUID().toString());

        notification.setCreateTime(LocalDateTime.now());
        // 管理员通知，推送给所有用户
        notification.setUserId(userService.getUserInfo(token).getUserId());
        notification.setTargetUserId(null);

        this.baseMapper.insert(notification);
        sendMessageService.broadcast(notification.getTitle());
        return Result.success("添加成功");
    }

    @Override
    public Result<?> addSystemNotice(String userId, String num, String content) {
        // 系统通知，推送给目标用户

        Notification notice = new Notification();
        notice.setId(UUID.randomUUID().toString());
        notice.setTitle("系统通知");
        notice.setContent("<p>单号：" + num + "</p><br><p>" + content + "</p>");

        // 查询目标用户
        notice.setId(UUID.randomUUID().toString());
        notice.setTargetUserId(userId);
        notice.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(notice);

        sendMessageService.pushMessage(userId, content);


        return Result.success("添加成功");
    }

    @Override
    public Result<?> selectPage(Integer pageNo, Integer pageSize, String token) {
        LoginUser userInfo = userService.getUserInfo(token);

        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notification::getTargetUserId, userInfo.getUserId()).or().isNull(Notification::getTargetUserId);

        queryWrapper.orderByDesc(Notification::getCreateTime);

        Page<Notification> notificationPage = this.baseMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);


        Integer total = (int) notificationPage.getTotal();


        List<Notification> notifications = notificationPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("rows", notifications);
        data.put("total", total);
        return Result.success(data);
    }
}
